//****************************************************************************
// Copyright (c) 2011-2013,WebJet Business Division,CYOU

// http://www.genesis-3d.com.cn

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************
Shader "PostEffect_SunShaft0" {
	Parameters{	
		Vector [texToTexParams] = "0.00066713482, 0.00036231885, -0.00017556180, 0.0013913043"

		Vector [SunShafts_SunPos] = "30.000000,60.387765,0,1"
		Vector [SunShafts_SunCol]   = "0.8,0.8,0.6,1"

		Vector [PI_sunShaftsParam1] = "0.1,0.76319116,0.0,0.0"
		Vector [PI_sunShaftsParam4] = "0.0050584702,1.1844447,0.0,0.0"
		
		sampler2D [p4RT] = "white"
	}
	
	RenderQueue "CUSTOMDRAW"
	
	

			CODEBEGIN

			float4 texToTexParams;

			struct VS_OUTPUT
			{
				float4 Position		: POSITION0;   // vertex position 
				float4 PosTex		: TEXCOORD0;//xy is positon, and zw is texCoords coord
			};
			struct PS_INPUT
			{
				float4 PosTex		: TEXCOORD0;
			};
			
			VS_OUTPUT mainVs(in float3 vPos : POSITION)
			{
					VS_OUTPUT Output;
									
					Output.Position.xy = vPos.xy;	
					Output.Position.zw = float2(0.9f, 1.0f);
					Output.PosTex.xy = vPos.xy;
					Output.PosTex.zw = 0;
					return Output;
			}

			float4 mainPs(PS_INPUT o) : COLOR0
			{
				  float2 uv = getUVfromscreenspace(o.PosTex.xy);
				  float4 scene0 = tex2D(g_MainBuffer, uv);
				  float4 scene1 = tex2D(g_MainBuffer, uv+texToTexParams.xy);
				  float4 scene2 = tex2D(g_MainBuffer, uv+texToTexParams.zw);
				  float4 scene3 = tex2D(g_MainBuffer, uv+( texToTexParams.xy*float2(-1,-1) ) );
				  float4 scene4 = tex2D(g_MainBuffer, uv+( texToTexParams.zw*float2(-1,-1) ) );
				  
				  float4 scene = (scene0 + scene1 + scene2 + scene3 + scene4) * 0.2;
				  
				  float4 sceneDepth = tex2D(g_DepthMap, uv);
				  
				  //-hack :now the sky box depth is 0,assign it to 1 here;
				  if(sceneDepth.r<0.000001)
				  {
					  sceneDepth.r = 1.0;
			      }
				  
				  return float4(scene.x * sceneDepth.r,scene.y*sceneDepth.r,scene.z* sceneDepth.r, 1 - sceneDepth.r);
			}
			CODEEND

	Technique "PostEffect_SunShaft0"{
		Pass {
			Compiler{
				VertexShader = mainVs
				PixelShader = mainPs
			}
		
			RenderSTATE { 
							cullMode "Back"
							ZTest Off
							ZWrite Off
							blendMode Off
							alphatest Off
							samplerState "p4RT" [Clamp] [Linear]
							samplerState "g_MainBuffer" [Clamp] [Point]
							samplerState "g_DepthMap" [Clamp] [Point]
				    }
		}
	}
}



                                                                                                               